package com.xixibaby.core.message;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public abstract class BaseChannelHandler<T> implements ChannelHandler {

    @Override
    public List<Boolean> handler(String[] target, Map<String, Object> context) {
        T t = this.convertToParam(context);
        this.log(target,t);
        if(target.length > 1){
           return this.handler(target,t);
        }else{
            String tar = target[0];
            Boolean handler = this.handler(tar, t);
            return Collections.singletonList(handler);
        }
    }

    public abstract T convertToParam(Map<String, Object> context);

    public void log(String[] target,T t){
        log.info("采用本地消息: 【{}】【{}】",String.join(",",target), JSONObject.toJSONString(t));
    }

    protected abstract boolean handler(String target, T param);

    protected List<Boolean> handler(String[] target, T param){
       return Arrays.stream(target).map(x -> this.handler(x,param)).collect(Collectors.toList());
    }
}
